home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Belgian Amiga Club - ADF Collection
/
BS1 part 41.zip
/
BS1 part 41
/
Compute`s Amiga resource 1.adf
/
Source
/
AskRobPeck
/
gad.c
Wrap
C/C++ Source or Header
|
1989-02-07
|
6KB
|
222 lines
/* gad.c */
#include "exec/types.h"
#include "intuition/intuition.h"
#include "exec/memory.h"
char buffer;
ULONG wakeupbit, cx, cy, maxw, maxh;
USHORT cl, co, qu, mo, mx, my;
APTR ad; /* address of the gadget which we hit */
char *phrases[] = { "item1","item2","item3","item4",
"item5","item6","item7","item8","item9","my10","my11",
"my12","my13","my14","my15","my16","my17","my18","my19",
"my20","my21","my22","my23","my24","my25","my26","my27",
"my28","my29",0,0,0,0,0,0,0,0,0,0,0 }; /* no text last 10 */
struct Window *w, *OpenWindow();
struct RastPort *rp;
struct IntuiMessage *mg;
struct Message *GetMsg();
struct Library *IntuitionBase, *GfxBase, *OpenLibrary();
#define SAVEMSG {cl=mg->Class;co=mg->Code;qu=mg->Qualifier;\
ad=mg->IAddress;mx=mg->MouseX;my=mg->MouseY;\
ReplyMsg(mg);}
#define MAXCHARS 5 /* how many chars in each box */
#define TOP 52 /* where to begin from GZZwindow top */
#define LEFT 16 /* where to begin from GZZwin left */
#define CWIDTH 8 /* width of character (using TOPAZ 8) */
#define WWIDTH (MAXCHARS * CWIDTH) /* width of word, 5 char max */
#define HSPACE ((CWIDTH * 2) + WWIDTH) /* horizontal space/gad */
#define HEIGHT 8 /* height of TOPAZ 8 is 8 lines */
#define VSPACE (HEIGHT * 2 - 1) /* vertical space between boxes */
#define MIN(a,b) (a < b ? a : b)
struct Gadget abbrev_select = { /* starts relative to GZZ window */
NULL, 12,47, 568,68, GADGHNONE, RELVERIFY+GADGIMMEDIATE,
BOOLGADGET,NULL,NULL,NULL,0,NULL,99, NULL };
struct NewWindow nw = {
18,12,597,147,0,1,GADGETDOWN+CLOSEWINDOW,
WINDOWDRAG+WINDOWCLOSE+SMART_REFRESH+GIMMEZEROZERO,
&abbrev_select,NULL,(UBYTE *) "Multi-Gadget Test",
NULL,NULL,100,50,640,200,WBENCHSCREEN };
/* convert win-rel mouse-clicks to main gadget internal x,y */
/* NOTE: based on a GZZ window!! */
#define GY -17+my-abbrev_select.TopEdge
#define GX -9+mx-abbrev_select.LeftEdge
char *itoa(x)
int x;
{
buffer = (char)(0x30 + x);
return(&buffer);
}
void DrawBox(x, y) /* draw it downward and to the right */
short x,y;
{
SetDrMd(rp,JAM2);
SetAPen(rp,3);
Move(rp,x-1,y-1);
Draw(rp,x+WWIDTH+1, y-1);
Draw(rp,x+WWIDTH+1, y+HEIGHT+1);
Draw(rp,x-1, y+HEIGHT+1);
Draw(rp,x-1, y);
}
void ShowCursor(x,y) /* Same as drawbox but fill it */
long x,y;
{
SetDrMd(rp, COMPLEMENT);
/* De-highlite old "gadget". */
RectFill(rp, cx, cy, cx+WWIDTH, cy+HEIGHT);
/* highlight new "gadget" position */
RectFill(rp, x, y, x+WWIDTH, y+HEIGHT);
cx = x;
cy = y; /* establish a new current position */
}
void DrawGads() /* put text into the "gadgets" we created */
{
long i, j, n, len;
maxw = 568 / HSPACE; /* max gadget subdivisions across */
maxh = 68 / VSPACE; /* max gadget subdivs high in real gadget */
cy = TOP; /* start at the top "gadget" */
n = 0; /* select phrases one at a time */
for(j=0; j<maxh; j++) {
cx = LEFT;
for(i=0; i<maxw; i++) {
SetDrMd(rp,JAM2);
SetAPen(rp,1);
Move(rp,cx+1, cy+6);
len = strlen(phrases[n]);
Text(rp,phrases[n], MIN(len,MAXCHARS));
DrawBox(cx, cy); /* box around the "gadget" */
cx += HSPACE;
n += 1;
}
cy += VSPACE;
}
}
void main(argc, argv)
long argc;
char *argv[];
{
int result;
GfxBase = (struct Library *)OpenLibrary("graphics.library", 0);
if (GfxBase == NULL)
goto cleanup;
IntuitionBase = (struct Library *)OpenLibrary("intuition.library", 0);
if(IntuitionBase == NULL)
goto cleanup;
w = OpenWindow(&nw); /* open a window */
rp = w->RPort;
DrawGads(); /* draw a defined number of "gadgets" */
cx = LEFT;
cy = TOP;
SetDrMd(rp, COMPLEMENT);
RectFill(rp, cx, cy, cx+WWIDTH, cy+HEIGHT); /* hilite first */
while(1) {
WaitPort(w->UserPort);
mg = (struct IntuiMessage *)GetMsg(w->UserPort);
while(mg) { /* empties the port before going to sleep again */
SAVEMSG
if((result = HandleEvent()) == 0)
break;
mg = (struct IntuiMessage *)GetMsg(w->UserPort);
}
if(result == 0)
break;
}
cleanup:
if(w)
CloseWindow(w);
if(GfxBase)
CloseLibrary(GfxBase);
if(IntuitionBase)
CloseLibrary(IntuitionBase);
}
int DumpXY()
{
long x_index, y_index, word_index;
long x, y, mm;
/* NOTE: calculations based on GZZ window! */
/* convert to coordinates relative to topleft of gadget */
x_index = (GX) /HSPACE;
y_index = (GY) /VSPACE;
if(x_index>maxw-1 || y_index>maxh-1 || x_index<0 || y_index<0)
return(-1);
/* within the box? */
if(GX - (x_index * HSPACE) > WWIDTH)
return(-1);
if(GY - (y_index * VSPACE) > HEIGHT)
return(-1);
x = LEFT+ (x_index * HSPACE);
y = TOP + (y_index * VSPACE);
word_index = (y_index * maxw) + x_index;
mm = 1 + word_index; /* which gadget number was that? */
if(strlen(phrases[mm-1]) == 0)
return(-1); /* inactive if no text */
ShowCursor(x,y); /* only one gadget selectable at a time */
return(mm); /* tell caller which gadget number was hit */
}
int HandleEvent()
{
int testcode, num;
SHORT gadgetid;
testcode = 1;
switch(cl)
{
case CLOSEWINDOW:
return(FALSE);
break;
case GADGETDOWN:
gadgetid = ((struct Gadget *)ad)->GadgetID;
switch(gadgetid)
{
case 99:
num = DumpXY(); /* num, if needed */
SetDrMd(rp,JAM2);
SetAPen(rp,1);
Move(rp,20,40);
if(num == -1) {
Text(rp,"Outside range of active gadgets ",32);
break;
}
else {
Text(rp,"You just hit gadget number ",32);
Move(rp,244,40);
if(num <= 9) {
Text(rp,itoa(num),1);
}
else if(num >=10 && num <= 19) {
Text(rp,itoa(1),1);
Text(rp,itoa(num-10),1);
}
else if(num >=20 && num <= 29) {
Text(rp,itoa(2),1);
Text(rp,itoa(num-20),1);
}
else if(num >=30 && num <= 39) {
Text(rp,itoa(3),1);
Text(rp,itoa(num-30),1);
}
}
default:
break;
} /* end of gadget switch */
cl = 0; /* null the event */
default:
break;
} /* end of switch cl */
return(testcode);
}